//
//  HMTitleButton.swift
//  SinaWeiBo
//
//  Created by immncjykeny on 2017/5/9.
//  Copyright © 2017年 immncjykeny. All rights reserved.
//

import UIKit

class HMTitleButton: UIButton {
    //这不是重写父类方法 而是重载构造函数
    init(title:String?){
        
        super.init(frame: CGRect())
        
        if title == nil {
            
            setTitle("首页 ", for: .normal)
        }else {
            
            setTitle(title!, for: .normal)
            
           // setTitle(title! + " ", for: .normal)
            
            
            setImage(UIImage(named:"navigationbar_arrow_down"), for: .normal)
            
            setImage(UIImage(named:"navigationbar_arrow_up"), for: .selected)
            
            
            
        }
        //字体加粗
        titleLabel?.font = UIFont.boldSystemFont(ofSize: 17)
        
        setTitleColor(UIColor.darkGray, for: .normal)
        //自适应大小
        sizeToFit()
        
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //重新布局子视图
    override func layoutSubviews() {
        super.layoutSubviews()
        
        guard let titleLabel = titleLabel,
              let imageView = imageView else{
                return
        }
        print("\(titleLabel) \(imageView)")
        //label 的X就是image 的宽度
//        titleLabel.frame = titleLabel.frame.insetBy(dx: -imageView.size.width, dy: 0)
        
        var titleFrame = titleLabel.frame
        
       titleFrame.origin.x = -imageView.bounds.width
        
        titleLabel.frame = titleFrame
        //image的X就是label的宽度
        var imageframe = imageView.frame
        
        imageframe.origin.x = titleLabel.bounds.width - 10
        
        imageView.frame = imageframe
        
    }
    

}
